package com.eachonline.smart.web.feekback;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartFeedBack;
import com.eachonline.smart.model.SmartFeedBackExample;
import com.eachonline.smart.model.base.BaseSmartFeedBackExample.Criteria;
import com.eachonline.smart.service.FeedBackService;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;


@Controller
@RequestMapping("/feedback")
public class FeekBackController {

	@Autowired
	public FeedBackService  feedbackService;
	
	/**
	 * 意见反馈
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public JsonResult saveComment(HttpServletRequest request, SmartFeedBack  feedback){
		
		if(StringUtil.isEmpty(feedback.getTitle()))
		{
			return  new JsonResult(false, "标题不能为空");
		}
		if(StringUtil.isEmpty(feedback.getContent()))
		{
			return  new JsonResult(false, "内容不能为空");
		}
		
		if(StringUtil.isEmpty(feedback.getContact()))
		{
			return  new JsonResult(false, "联系方式不能为空");
		}
		if(StringUtil.isEmpty(feedback.getUserName()))
		{
			return  new JsonResult(false, "用户名不能为空");
		}
		
		feedback.setSid(UUIDUtil.getUUID());
		feedback.setCreateTime(new Date());
		int  row =feedbackService.insert(feedback);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else{
			return   new JsonResult(true, "请稍后再试");
		}
	}
	
	
	
	
	/**
	 * 意见反馈列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/queryList",method = RequestMethod.POST)
	public JsonResult queryFeedBackList(HttpServletRequest request,String feedType){
		

		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		SmartFeedBackExample  fbex=new SmartFeedBackExample();
		
		Criteria  fbexCri=fbex.createCriteria();
		
		if(StringUtil.isNotEmpty(feedType))
		{
			fbexCri.andFeedTypeEqualTo(feedType);
		}
		Pagination<SmartFeedBack> feedBackPageInfo= feedbackService.queryByPage(fbex, begin, pageSize," create_time desc");
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("feedBackPageInfo", feedBackPageInfo);
		return new JsonResult(true, "成功", resultMap);
	
		
	}
		
	
	
	
	/**
	 * 意见反馈详情
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/queryDetail",method = RequestMethod.POST)
	public JsonResult queryFeedBackDetail(HttpServletRequest request,String sid){
		
		SmartFeedBack smartFeedBack= feedbackService.selectByPrimaryKey(sid) ;
		if(smartFeedBack==null)
		{
			return new JsonResult(false, "不存在反馈信息");
		}
		
		Map<String,Object> resultMap=new HashMap<String, Object>();
		resultMap.put("feedBack", smartFeedBack);
		return new JsonResult(true, "成功", resultMap);  
	}
		
	/**
	 * 意见反馈删除
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manage/delete",method = RequestMethod.POST)
	public JsonResult deleteFeedBack(HttpServletRequest request,String sid){
		
		int row =feedbackService.deleteByPrimaryKey(sid);
		
		if(row>0)
		{
			return new JsonResult(true, "成功");  
		}else
		{
			return new JsonResult(false, "成功");  
		}
	}
		
}
